#include <gui/MainWindow.h>

#include <QDesktopWidget>
#include <QDebug>
#include <QApplication>
#include <QPainter>
#include <QEvent>
#include <QMouseEvent>
#include <QPalette>
#include <QPixmap>
#include <QString>
#include <QLabel>
#include <QBrush>
#include <QCursor>
#include <QSize>
#include <QVBoxLayout>
#include <iostream>
#include <iomanip>
#include <QTextEdit>
#include <QStatusBar>
#include <QProgressBar>

#include <core/ApplicationResources.h>

MainWindow::MainWindow() : QMainWindow() {
    setWindowTitle("Gesture Interface");

    resize(QSize(800, 600));    
    setFixedSize(width(), height());

    move((QApplication::desktop()->width() - this->width()) / 2, (QApplication::desktop()->height() - this->height()) / 2);

//    setCursor(QCursor(Qt::BlankCursor));

    QPalette palette;
    palette.setBrush(QPalette::Background, QBrush(QPixmap(ApplicationResources::images::MAIN_WINDOW_BACKGROUND)));
    setPalette(palette);

    setMouseTracking(true);

    installEventFilter(this);

    qtLabel = new QLabel(this);
    qtLabel->setPixmap(QPixmap(ApplicationResources::images::OPENED_CURSOR_IMAGE));
    qtLabel->setGeometry(0, 0, 30, 30);

    isReached = false;

    box = new QLabel(this);
    box->setPixmap(QPixmap(ApplicationResources::images::BOX));
    box->setAlignment(Qt::AlignCenter);
    box->setGeometry(0, 500, 100, 100);
}

MainWindow::~MainWindow() {
}

bool MainWindow::eventFilter(QObject * qtObject, QEvent * qtEvent) {
    if(qtEvent->type() == QEvent::MouseMove) {

        QMouseEvent * qtMouseEvent = static_cast<QMouseEvent *>(qtEvent);

        qDebug() << "x: " << qtMouseEvent->x() << "/" << qtMouseEvent->globalX() << " y: " << qtMouseEvent->pos().y() << "/" << qtMouseEvent->globalY();

        qtLabel->setAlignment(Qt::AlignCenter);

        if (qtLabel->y() + qtLabel->height() > 500) {
            qtLabel->hide();
        } else {
            qtLabel->show();
        }

        qtLabel->move(qtMouseEvent->x(), qtMouseEvent->y());
//        qtLabel->move(qtMouseEvent->globalX() - x() - qtLabel->width() / 2, qtMouseEvent->y() - qtLabel->height() / 2);
    }

    return QObject::eventFilter(qtObject, qtEvent);
}

void MainWindow::mousePressEvent(QMouseEvent * qtMouseEvent) {
    qtLabel->setPixmap(QPixmap(ApplicationResources::images::BOMB));
//    qtLabel->setPixmap(QPixmap(ApplicationResources::images::CLOSED_CURSOR_IMAGE));
}

void MainWindow::mouseReleaseEvent(QMouseEvent * qtMouseEvent) {
    qtLabel->setPixmap(QPixmap(ApplicationResources::images::OPENED_CURSOR_IMAGE));
}

void MainWindow::wheelEvent(QWheelEvent *qtWheelEvent) {
    qDebug() << qtWheelEvent->type();
}

void MainWindow::dragEnterEvent(QDragEnterEvent * e) {
    qDebug() << "He";
}

void MainWindow::paintEvent(QPaintEvent * e) {
     QPainter *painter = new QPainter(this);
//     painter->setBrush(Qt::gray);
//     painter->drawRect(100, 100, 200, 200);


//     painter->setBrush(Qt::gray);
//     painter->drawRect(100, 100, 200, 200);

//     painter->setBrush(Qt::gray);
//     painter->drawEllipse(qtLabel->pos(), 5, 5);

     if (!qtLabel->isVisible()) {
         box->hide();
        painter->setBrush(Qt::gray);
        painter->drawText(QRect(100, 100, 50, 50), "djkfjdsjfklsjdkjfskdjfext");
//         painter->drawPixmap(0, 500, QPixmap(ApplicationResources::images::BOX));
     }

//     if (qtLabel->isVisible()) {
//         painter->drawPixmap(0, 500, QPixmap(ApplicationResources::images::BOX));
//     } else {
//        painter->setBrush(Qt::gray);
//        painter->drawRect(100, 100, 200, 200);
//     }

//     if (!isReached) {
//        painter->drawPixmap(0, 500, QPixmap(ApplicationResources::images::BOX));
//     } else {
//        painter->setBrush(Qt::gray);
//        painter->drawRect(100, 100, 200, 200);
////        painter->drawText(0, 500, "Text");
//     }
//     painter->setBrush(Qt::gray);
//     painter->drawRect(100, 100, 200, 200);
}
